package org.puremvc.as3.multicore.utilities.unifiedloader.clients.flash
{

/*
 	PureMVC UnifiedLoader Utility
 	Copyright (c) 2008 neil manuell
  	Your reuse is governed by the Creative Commons Attribution 3.0 License
 */
	
	import flash.display.*;
	import flash.events.*;
	
	import org.puremvc.as3.multicore.utilities.unifiedloader.clients.LoaderClientBase;
	import org.puremvc.as3.multicore.utilities.unifiedloader.interfaces.ILoaderClientInfo;
	import org.puremvc.as3.multicore.utilities.unifiedloader.patterns.deconstructors.DisplayObjectDeconstructor;
	import org.puremvc.as3.multicore.utilities.unifiedloader.patterns.factories.DisplayObjectFactory;
	

	
	public class LoaderClient extends LoaderClientBase
	{
		public static const NAME:String = "loaderClient";
		
		private var mLoader:Loader;
		private var mLoadInfo:LoaderInfo;
		
		public function LoaderClient(cli:ILoaderClientInfo)
		{
			super(cli);
		}

		
		override protected function initialiseLoad():void
		{
			mLoader = new Loader();
			mLoadInfo = mLoader.contentLoaderInfo;
		}
		
		override protected function startLoad():void
		{
			mLoader.load(loaderClientInfo.urlRequest, loaderClientInfo.loaderContext);
		}
		
		override protected function prepareAsset(event:Event):void
		{
			var content:DisplayObject,
				factory:DisplayObjectFactory,
				loaderInfo:LoaderInfo;
			
			loaderInfo = LoaderInfo(event.target);
			
			content = loaderInfo.content;

			factory = new DisplayObjectFactory(content);
			loaderClientInfo.deconstructor = new DisplayObjectDeconstructor(content);
			loaderClientInfo.setAsset(factory);
			
			
		}
		
		override protected function addListeners():void
		{
			mLoadInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);
			mLoadInfo.addEventListener(Event.INIT, relayHandler);
			mLoadInfo.addEventListener(ProgressEvent.PROGRESS, relayHandler);
			mLoadInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			mLoadInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, relayHandler);
		}
		
		override protected function removeListeners():void{

			mLoadInfo.removeEventListener(Event.COMPLETE, loadCompleteHandler);
			mLoadInfo.removeEventListener(Event.INIT, relayHandler);
			mLoadInfo.removeEventListener(ProgressEvent.PROGRESS, relayHandler);
			mLoadInfo.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			mLoadInfo.removeEventListener(HTTPStatusEvent.HTTP_STATUS, relayHandler);

		}
		
		
		
		
			
		

		
		
		
	}
}

